<?php

namespace app\index\controller;

use app\admin\model\Ab as AbModel;
use app\admin\model\AbLog as AbLogModel;
use app\admin\model\Country as CountryModel;
use app\admin\model\Order as OrderModel;
use app\common\controller\Frontend;
use think\Route;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $name = $this->request->param('name');
        $one = AbModel::with(['landingpage'])->where('name', $name)->field('id,admin_id,pixel,event_name,content_type,review_url,landingpage_id,ip_limit,country_ids,white_ip_list')->find();

        if ($one) {
            // 先判断是否设置 IP 屏蔽

            $is_limit = false;

            // 如果当不前 ip 在设置的白名单里，并且 ip_limit 设置为 country 模式
            if (!in_array(request()->ip(), explode(",", $one['white_ip_list'])) && $one['ip_limit'] === 'country') {
                // ip 换国家
                $csmip = \addons\csmip\library\Csmip::getInstance();
                $region = $csmip->getRegion(request()->ip());
                $country = $region->country;

                // 当前 ip 在国家表里的 id
                $cur_country = CountryModel::where('name', $country)->field('id')->find();


                if (!$cur_country) {
                    // 如果当前 ip 不在国家表里
                    $is_limit = true;
                } else if ($cur_country && !in_array($cur_country['id'], explode(",", $one['country_ids']))) {
                    // 如果当前 ip 在国家表里的 id 存在，并且不在国家 id 里
                    $is_limit = true;
                }
            }

            if ($is_limit) {
                if ($one['review_url'] !== '') {
                    return redirect($one['review_url'] . '?f=1', [], 302);
                } else {
                    return $this->view->fetch('error');
                }
            } else {
                if ($one['content_type'] === 'review') {
                    if ($one['review_url'] !== '') {
                        return redirect($one['review_url'] . '?f=2', [], 302);
                    } else {
                        return $this->view->fetch('error');
                    }
                }

                // 轮训抽取 whatsapp id
                $order = OrderModel::where('admin_id', $one['admin_id'])
                    ->where('status', 2)
                    ->where('whatsup_status', 2)
                    ->where('admin_whatsup_status', 2)
                    ->order('display_num', 'asc')
                    ->order('weigh', 'desc')
                    ->select();

                // 如果抽不到
                if (!$order) {
                    if ($one['review_url'] !== '') {
                        return redirect($one['review_url'] . '?f=3', [], 302);
                    } else {
                        return $this->view->fetch('error');
                    }
                }

                // 存一次记录
                $abLog = new AbLogModel();
                $abLog->save([
                    'ab_id' => $one['id'],
                    'order_id' => $order[0]['id'],
                ]);

                // diplay_num + 1
                $updateOrder = OrderModel::get($order[0]['id']);
                $updateOrder->save(['display_num' => $updateOrder['display_num'] + 1]);

                $this->view->assign('order_id', $order[0]['id']);
                $this->view->assign('pixel', $one['pixel']);
                $this->view->assign('event_name', $one['event_name']);
                return $this->view->fetch($one['landingpage']['path']);
            }
        } else {
            return $this->view->fetch('error');
        }
    }
}
